// 全局公共方法
import base from '@/config/baseUrl';
const install = (Vue, vm) => {

	// 登录操作
	const login = (userInfo) => {
		console.log('login',userInfo)
		vm.$u.vuex('userInfo', userInfo)
		vm.$u.vuex('accessToken', userInfo.token)
		vm.$u.vuex('isLogin', true)
		uni.switchTab({
			url: '/pages/coin/index'
		})
	}

	// 退出登录
	const logout = () => {
		vm.$u.vuex('userInfo', {
			avatar: '',
			username: '游客'
		})
		vm.$u.vuex('accessToken', '')
		vm.$u.vuex('isLogin', false)
		uni.redirectTo({
			url: '/pages/account/login'
		})
	}

	const isParamEmpty = (params) => {
		let flag = false;
		for(var key in params){
			if(params[key] != '0' && !params[key]){
				return true;
			}
		}
		return flag;
	}

	// 跳转路由前检查登录状态
	const route = (url) => {
		// console.log('route', vm, vm.user.isLogin)
		if (!vm.user.isLogin) {
			uni.showToast({
				title: '请先登录',
				icon: 'none'
			})
			setTimeout(() => {
				uni.navigateTo({
					url: '/pages/account/login'
				})
			}, 500)
			return false
		}
		uni.navigateTo({
			url: url
		})

	}

	const route2 = (url) => {
		/* if (!vm.user.isLogin) {
			uni.showToast({
				title: '请先登录',
				icon: 'none'
			})
			setTimeout(() => {
				uni.navigateTo({
					url: '/pages/login/login-account'
				})
			}, 500)
			return false
		} */
		uni.switchTab({
			url: url
		})
	}

  // 提示
	const showToast = (data = {}) => {
		if (typeof data == 'string') {
			uni.showToast({
				title: data,
				icon: 'none'
			})
		} else {
			uni.showToast({
				title: data.title,
				icon: data.icon || 'none',
				image: data.image || '',
				mask: data.mask || false,
				position: data.position || 'center',
				duration: data.duration || 1500,
				success: () => {
					setTimeout(() => {
						if (data.back) return uni.navigateBack()
						data.success && data.success()
					}, data.duration || 1500)
				}
			})
		}
	}

	var CryptoJS = require('crypto-js');
	var HmacSHA256 = require('crypto-js/hmac-sha256')
	
	const sign_sha = (method, baseurl, path, data) =>{
    var pars = [];
    for (let item in data) {
        pars.push(item + "=" + encodeURIComponent(data[item]));
    }
    var p = pars.sort().join("&");
    var meta = [method, baseurl, path, p].join('\n');
    // console.log(meta);
    var hash = HmacSHA256(meta, base.huobi.secret_key);
    var Signature = encodeURIComponent(CryptoJS.enc.Base64.stringify(hash));
    // console.log(`Signature: ${Signature}`);
    p += `&Signature=${Signature}`;
    // console.log(p);
    return p;
	}

	const get_sign = (method, baseurl, path, data) =>{
    var pars = [];
    for (let item in data) {
        pars.push(item + "=" + encodeURIComponent(data[item]));
    }
    var p = pars.sort().join("&");
    var meta = [method, baseurl, path, p].join('\n');
    // console.log(meta);
    var hash = HmacSHA256(meta, base.huobi.secret_key);
    var Signature = encodeURIComponent(CryptoJS.enc.Base64.stringify(hash));
    return Signature;
	}

const moment = require('moment')
const get_body = () => {
	return {
			AccessKeyId: base.huobi.api_key,
			SignatureMethod: "HmacSHA256",
			SignatureVersion: 2,
			Timestamp: moment.utc().format('YYYY-MM-DDTHH:mm:ss'),
	};
}


  // 将定义的方法挂载，使用this.$u.func.xx调用
	Vue.prototype.$u.func = {
		login,
		logout,
		isParamEmpty,
		route,
		route2,
		showToast,
		sign_sha,
		get_body,
		get_sign
	}
}
export default {
	install
}